package com.ecoolex.chinadutyfree.retailer.job.scheduler;

import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ecoolex.chinadutyfree.retailer.core.manager.PaymentOrderManager;
import com.ecoolex.chinadutyfree.retailer.entity.PaymentOrder;
import com.ecoolex.framework.scheduler.Job;
import com.ecoolex.framework.scheduler.JobComponent;

@JobComponent
public class PaymentOrderCheckJob {

	private static final Logger log = LoggerFactory.getLogger(PaymentOrderCheckJob.class);
	
	@Autowired
	private PaymentOrderManager paymentOrderManager;
	
	//@Job(name = "chinadutyfree.PaymentOrderCheckJob.checkPayStatus", describe = "检查支付订单的支付状态")
    public void checkPayStatus() {
        // 5秒的间隔查询时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, -5);
        List<PaymentOrder> orderList = paymentOrderManager.queryPayApplyTimeout(calendar.getTime(), 10);

        orderList.forEach(order -> {
            try {
            	paymentOrderManager.checkPayStatus(order, this.getClass().getSimpleName());
            } catch (Exception e) {
                log.error("检查支付订单的支付状态异常. paymentOrderId={}", order.getPaymentOrderId(), e);
            }
        });
    }

    //@Job(name = "chinadutyfree.PaymentOrderCheckJob.checkRefundStatus", describe = "检查支付订单的退款状态")
    public void checkRefundStatus() {
        // 5秒的间隔查询时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, -5);
        List<PaymentOrder> orderList = paymentOrderManager.queryRefundApplyTimeout(calendar.getTime(), 10);

        orderList.forEach(order -> {
            try {
            	paymentOrderManager.checkRefundStatus(order, this.getClass().getSimpleName());
            } catch (Exception e) {
                log.error("检查支付订单的退款状态异常. paymentOrderId={}", order.getPaymentOrderId(), e);
            }
        });
    }
}
